/**
 * Created by forcht on 2018/7/13.
 */
public class Solution {
    int maxValue = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        if (root==null)return 0;
        maxSubPathSum(root);
        return maxValue;
    }

    public int maxSubPathSum(TreeNode root) {
        if (root == null) return 0;
        int left = Math.max(0, maxSubPathSum(root.left));
        int right = Math.max(0, maxSubPathSum(root.right));
        maxValue = Math.max(maxValue, left + right + root.val);
        return Math.max(left, right) + root.val;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
